package cn.sunlelin.educationserver.controller;

import cn.sunlelin.educationserver.entity.Discuss;
import cn.sunlelin.educationserver.mapper.DiscussMapper;
import cn.sunlelin.educationserver.service.DiscussService;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@CrossOrigin
public class DiscussController {
    @Autowired
    private DiscussService discussService;

    @PostMapping("/discuss/createDiscuss")
    public Map<String,Object> createDiscuss(@RequestBody Discuss discuss){
        Map<String,Object> map=new HashMap<>();
        if(discussService.createDiscuss(discuss)){
            map.put("code",200);
            map.put("msg","发布讨论成功");
            map.put("dataobject",null);
        }
        return map;
    }
    @GetMapping("/discuss/selectDiscussByCourseId/{courseId}")
    public Map<String,Object> selectDiscussByCourseId(@PathVariable Integer courseId){
        Map<String,Object> map=new HashMap<>();
        List<Map<String,Object>> discussList = discussService.selectDiscussByCourseId(courseId);
        if(discussList.size()!=0){
            map.put("code",200);
            map.put("msg","课程Id查询讨论成功");
            map.put("dataobject",discussList);
        }
        else {
            map.put("code",500);
            map.put("msg","课程Id查询讨论失败");
            map.put("dataobject",null);
        }
        return map;
    }
    @GetMapping("/discuss/selectDiscussByUserId/{userId}")
    public Map<String,Object> selectDiscussByUserId(@PathVariable Integer userId){
        Map<String,Object> map=new HashMap<>();
        List<Map<String,Object>> discussList = discussService.selectDiscussByUserId(userId);
        if(discussList.size()!=0){
            map.put("code",200);
            map.put("msg","用户Id查询讨论成功");
            map.put("dataobject",discussList);
        }
        else {
            map.put("code",500);
            map.put("msg","用户Id查询讨论失败");
            map.put("dataobject",null);
        }
        return map;
    }
    @GetMapping("/course/getPageByUserId/{currentPage}/{pageSize}/{userId}")
    public Map<String,Object> getPageByUserId(@PathVariable Integer currentPage,
                                                @PathVariable Integer pageSize,
                                                @PathVariable Integer userId){
        if(currentPage==null) currentPage=1;
        if(pageSize==null)pageSize=2;
        Page<Map<String,Object>> page= discussService.getPageByUserId(currentPage,pageSize,userId);
        List<Map<String,Object>> pageList=page.getResult();
        long total = page.getTotal();
        Map<String,Object> m=new HashMap<>();
        m.put("pageList",pageList);
        m.put("total",total);
        Map<String,Object> map=new HashMap<>();
        map.put("code",200);
        map.put("msg","用户Id分页查询讨论成功");
        map.put("dataobject",m);
        return map;
    }
    @GetMapping("/discuss/selectDiscussById/{discussId}")
    public Map<String,Object> selectDiscussById(@PathVariable Integer discussId){
        Map<String,Object> map=new HashMap<>();
        Map<String,Object> discuss = discussService.selectDiscussById(discussId);
        if(discuss!=null){
            map.put("code",200);
            map.put("msg","Id查询讨论成功");
            map.put("dataobject",discuss);
        }
        else {
            map.put("code",500);
            map.put("msg","Id查询讨论失败");
            map.put("dataobject",null);
        }
        return map;
    }
    @GetMapping("/course/getPageByCourseId/{currentPage}/{pageSize}/{courseId}")
    public Map<String,Object> getPageByCourseId(@PathVariable Integer currentPage,
                                                    @PathVariable Integer pageSize,
                                                    @PathVariable Integer courseId){
        if(currentPage==null) currentPage=1;
        if(pageSize==null)pageSize=2;
        Page<Map<String,Object>> page= discussService.getPageByCourseId(currentPage,pageSize,courseId);
        List<Map<String,Object>> pageList=page.getResult();
        long total = page.getTotal();
        Map<String,Object> m=new HashMap<>();
        m.put("pageList",pageList);
        m.put("total",total);
        Map<String,Object> map=new HashMap<>();
        map.put("code",200);
        map.put("msg","课程Id分页查询讨论成功");
        map.put("dataobject",m);
        return map;
    }

    @GetMapping("/discuss/addBrowseNum/{discussId}")
    public Map<String,Object> addBrowseNum(@PathVariable Integer discussId){
        Map<String,Object> map=new HashMap<>();
        if(discussService.addBrowseNum(discussId)){
            map.put("code",200);
            map.put("msg","浏览数加1成功");
            map.put("dataobject",null);
        }
        else {
            map.put("code",500);
            map.put("msg","浏览数加1失败");
            map.put("dataobject",null);
        }
        return map;
    }

    @GetMapping("/discuss/addReplyNum/{discussId}")
    public Map<String,Object> addReplyNum(@PathVariable Integer discussId){
        Map<String,Object> map=new HashMap<>();
        if(discussService.addReplyNum(discussId)){
            map.put("code",200);
            map.put("msg","回复数加1成功");
            map.put("dataobject",null);
        }
        else {
            map.put("code",500);
            map.put("msg","回复数加1失败");
            map.put("dataobject",null);
        }
        return map;
    }

    @GetMapping("/discuss/minusReplyNum/{discussId}")
    public Map<String,Object> minusReplyNum(@PathVariable Integer discussId){
        Map<String,Object> map=new HashMap<>();
        if(discussService.minusReplyNum(discussId)){
            map.put("code",200);
            map.put("msg","回复数减1成功");
            map.put("dataobject",null);
        }
        else {
            map.put("code",500);
            map.put("msg","回复数减1失败");
            map.put("dataobject",null);
        }
        return map;
    }

    @GetMapping("/discuss/deleteDiscuss/{discussId}")
    public Map<String,Object> deleteDiscuss(@PathVariable Integer discussId){
        Map<String,Object> map=new HashMap<>();
        if(discussService.deleteDiscuss(discussId)){
            map.put("code",200);
            map.put("msg","删除讨论成功");
            map.put("dataobject",null);
        }
        else {
            map.put("code",500);
            map.put("msg","删除讨论失败");
            map.put("dataobject",null);
        }
        return map;
    }
}
